package com.bnz.manager.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.bnz.common.entity.R;
import com.bnz.manager.entity.group.Specification;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.bnz.manager.service.SpecificationService;
import com.bnz.common.utils.PageUtils;




/**
 * 
 *
 * @author Haziy
 * @email 1143878025@qq.com
 * @date 2023-02-16 14:12:33
 */
@RestController
@RequestMapping("/manager/specification")
public class SpecificationController {
    @Autowired
    private SpecificationService specificationService;

    /**
     * 列表
     */
    @GetMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = specificationService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * @author: zhanghao
     * @date: 2023/2/20 15:47
     * @param:
     * @return:
     * @功能: 查询规格列表
     **/
    @GetMapping("/findAll")
    public R findAll(){
        List<Map<String, String>> specList = specificationService.list().stream().map(m -> {
            Map<String, String> map = new HashMap<>();
            map.put("id", m.getId());
            map.put("text", m.getSpecName());
            return map;
        }).collect(Collectors.toList());
        return R.ok().put("specList", specList);
    }
    /**
     * 根据规格id查询出规格及规格选项列表
     */
    @GetMapping("/info/{id}")
    public R info(@PathVariable("id") String id) {

        Specification specification = specificationService.findById(id);

        return R.ok().put("specification", specification);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    public R save(@RequestBody Specification specification){
		specificationService.insert(specification);

        return R.ok();
    }

    /**
     * 修改
     */
    @PutMapping("/update")
    public R update(@RequestBody Specification specification){
		specificationService.update(specification);

        return R.ok();
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete")
    public R delete(@RequestBody String[] ids){
//		specificationService.removeByIds(Arrays.asList(ids));
        specificationService.deleteByIds(Arrays.asList(ids));
        return R.ok();
    }

}
